// src/services/bookService.js
import api from './api'
import { Book } from '@/models/Book'

export const bookService = {
  // 获取所有图书
  async getAllBooks() {
    const response = await api.get('/api/v1/books')
    return response.map(book => new Book(book))
  },

  // 根据ID获取图书
  async getBookById(id) {
    const response = await api.get(`/api/v1/books/${id}`)
    return new Book(response)
  },

  // 根据ISBN获取图书
  async getBookByIsbn(isbn) {
    const response = await api.get(`/api/v1/books/isbn/${isbn}`)
    return new Book(response)
  },

  // 根据作者获取图书
  async getBooksByAuthor(author) {
    const response = await api.get(`/api/v1/books/author/${author}`)
    return response.map(book => new Book(book))
  },

  // 根据分类获取图书
  async getBooksByGenre(genre) {
    const response = await api.get(`/api/v1/books/genre/${genre}`)
    return response.map(book => new Book(book))
  },

  // 创建图书
  async createBook(bookData) {
    const response = await api.post('/api/v1/books', bookData)
    return new Book(response)
  },

  // 借阅图书
  async borrowBook(bookId) {
    const response = await api.post(`/api/v1/books/${bookId}/borrow`)
    return new Book(response)
  },

  // 归还图书
  async returnBook(bookId) {
    const response = await api.post(`/api/v1/books/${bookId}/return`)
    return new Book(response)
  },

  // 删除图书
  async deleteBook(bookId) {
    await api.delete(`/api/v1/books/${bookId}`)
  }
}